package com.epalmpay.controller.channel.index;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import com.epalmpay.commom.BaseController;
import com.epalmpay.dto.member.MemberOutDTO;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.merchantmanage.GroupConfigOutDTO;
import com.epalmpay.entity.*;
import com.epalmpay.entity.profit.Profit;
import com.epalmpay.mapper.GroupConfigMapper;
import com.epalmpay.service.index.IIndexService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.dto.mall.MallGoodOutDTO;
import com.epalmpay.dto.merchant.index.GroupActivityBaseOutDTO;
import com.epalmpay.dto.merchant.index.GroupIndexInDTO;
import com.epalmpay.dto.merchant.index.GroupIndexListOutDTO;
import com.epalmpay.dto.merchant.index.StatisGroupDayOutDTO;

/** 
 * 说明：渠道首页
 * 创建人：gwn
 * 创建时间：2017-06-26
 */
@Controller
@RequestMapping(value="/merchant")
public class IndexController  extends BaseController{

	
	@Resource
	private IIndexService indexService;
	@Resource
	GroupConfigMapper groupConfigService;

	int[] month_days = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	
	
	@RequestMapping(value="/index")
	@ResponseBody
	public GroupIndexListOutDTO index(GroupIndexInDTO groupIndexInDTO){

		GroupIndexListOutDTO groupIndexListOutDTO= new GroupIndexListOutDTO();

		try {

			//查询昨日新增会员数
//			StatisGroupDay statisGroupDay = indexService.queryAddMemberSumGroup(groupIndexInDTO);
//			groupIndexListOutDTO.setStatisGroupDay(statisGroupDay);

			//查询今日新增会员数和会员总数
			Member member = indexService.queryAddMemberCount(groupIndexInDTO);
			groupIndexListOutDTO.setMember(member);

			//查询 本月新增代理商数 和总数
			groupIndexInDTO.setStatisDate(null);
			Agent agent = indexService.addAgentCount(groupIndexInDTO);
			groupIndexListOutDTO.setAgent(agent);

			//本月新增门店 门店总数
			groupIndexInDTO.setStatisDate(null);
			Shop shop =indexService.addShopCount(groupIndexInDTO);
			groupIndexListOutDTO.setShop(shop);

			//本月门店交易总额预计可得分润总额(本月分润金额)
//			StatisGroup statisGroup=indexService.queryMemberCountGroup(groupIndexInDTO);
//			groupIndexListOutDTO.setStatisGroup(statisGroup);
/*
			List<Shop> shopList = indexService.queryAllShopList(groupIndexInDTO.getGroupId());
			int amount=0;
			int profitAmount=0;
			for(Shop s:shopList){
				TradeOffline tradeOffline = indexService.queryShopTrade(s.getId());
				amount +=tradeOffline.getRealAmount();
				Profit profit = indexService.queryShopThisMonthProfit(s.getId());
				profitAmount +=profit.getShopProfit();
			}

			StatisGroup statisGroup = new StatisGroup();
			statisGroup.setThisMonthAmount(amount);
			statisGroup.setThisMonthProfitAmount(profitAmount);
			groupIndexListOutDTO.setStatisGroup(statisGroup);
*/


			//商户交易总额
			TradeOffline tradeOffline2=indexService.queryGroupTotalTradeAmount(groupIndexInDTO);
			groupIndexListOutDTO.setTradeOffline2(tradeOffline2);
			//分润总额
			Profit profit2 = indexService.queryGroupProfitAmount(groupIndexInDTO);
			groupIndexListOutDTO.setProfit2(profit2);

			//售后中的终端数量
			DevicePos devicePos = new DevicePos();
			int saleCount = indexService.queryDeviceSaleSum(groupIndexInDTO);//查询售后中终端
			devicePos.setSaleCount(saleCount);
			groupIndexListOutDTO.setDevicePos(devicePos);
			//终端数量 总数
			DevicePos devicePos2=indexService.queryDeviceSum(groupIndexInDTO);//查询终端的数量
			groupIndexListOutDTO.setDevicePos2(devicePos2);


			//今日目前交易金额(日交易总金额,日收入总金额)
//			List<GroupIndexOutDTO> groupOutDTOList = indexService.queryDayAmountGroup(groupIndexInDTO);
//			groupIndexListOutDTO.setGroupIndexOutDTOList(groupOutDTOList);

			//活动概况前5
			List<GroupActivityBaseOutDTO> groupActivityBaseOutDTOList =indexService.queryActivity(groupIndexInDTO);
			for (GroupActivityBaseOutDTO groupActivityBaseOutDTO : groupActivityBaseOutDTOList) {
				if (groupActivityBaseOutDTO.getEndTime()!=null) {
					Long time1= groupActivityBaseOutDTO.getEndTime().getTime();
					Long time2 = new Date().getTime();
					if (time1-time2>0 && time1!=253402185600000l) {
						groupActivityBaseOutDTO.setDay((int) ((time1-time2)/86400000));//得到天
						System.out.println(groupActivityBaseOutDTO.getDay());
					}else if (time1-time2<0  && time1!=253402185600000l) {
						groupActivityBaseOutDTO.setDay(-1);//表示已过期
					}else if( time1==253402185600000l){
						groupActivityBaseOutDTO.setDay(-2);
					}
				}else{
					groupActivityBaseOutDTO.setDay(-2);
				}

			}

			//今日目前交易额
			TradeOffline tradeOffline =indexService.queryDayAmountGroup(groupIndexInDTO);
			groupIndexListOutDTO.setTradeOffline(tradeOffline);
			//预计今日产生分润总额(所有代理商和)
			//ProfitAgent profitAgent = indexService.queryGroupPorfitAmount(groupIndexInDTO);
			//groupIndexListOutDTO.setProfitAgent(profitAgent);

			//预计今日产生分润总额(houjia)
			Profit profit = indexService.queryProfitAmount(groupIndexInDTO);
			groupIndexListOutDTO.setProfit(profit);


			groupIndexListOutDTO.setActivityBaseOutDTOList(groupActivityBaseOutDTOList);
		    //本月商品销量排行前5名
		    List<MallGoodOutDTO> mallGoodOutDTOList=indexService.queryGoodCountGroup(groupIndexInDTO);
		    groupIndexListOutDTO.setMallGoodOutDTOList(mallGoodOutDTOList);

		    //柱状图 //最近7天的数据
		 /*   List<StatisGroupDayOutDTO> statisGroupDayList= indexService.querySevenDayData(groupIndexInDTO);
		    groupIndexListOutDTO.setStatisGroupDayList(statisGroupDayList);*/


			//柱状图 //最近7天的数据
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

			List resultlist = new ArrayList();
			for(int i=6;i>=0;i--) {
				Map resultmap = new HashMap();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Calendar c = Calendar.getInstance();
				c.add(Calendar.DATE, - i);
				Date monday = c.getTime();
				String preMonday = sdf.format(monday);
				resultmap.put("date", preMonday);
				resultmap.put("shopmallNum", 0);
				resultmap.put("addMemberCount",0);

				resultlist.add(resultmap);
			}

			//查询最近7天新增会员人数
			List<MemberOutDTO> memberOutDTOList=indexService.queryWeekAddMemberCount(groupIndexInDTO);
			for(MemberOutDTO me:memberOutDTOList){
				String datet =dateFormat.format(me.getGmtCreate());
				for(int k=0;k<resultlist.size();k++) {
					Map map = (HashMap) resultlist.get(k);
					int addMember = me.getAddUserCount();
					if(map.containsValue(datet)) {//已经存在该日期
						System.out.print(datet);
						map.put("addMemberCount", map.get("addMemberCount")==null?addMember:Integer.parseInt(map.get("addMemberCount").toString()) + addMember);
					}

				}
			}
			//查询最近7天的订单数量
			//查询服务商品订单最近7天的数量
			List<OrderServiceGoodDetail> orderServiceGoodDetailList=indexService.queryWeekOrderCount(groupIndexInDTO);

			for(OrderServiceGoodDetail orderServiceGoodDetail:orderServiceGoodDetailList){
				String datet =dateFormat.format(orderServiceGoodDetail.getGmtCreate());
				for(int k=0;k<resultlist.size();k++) {
					Map map = (HashMap) resultlist.get(k);
					int orderNum = orderServiceGoodDetail.getNumber();
					if(map.containsValue(datet)) {//已经存在该日期
						System.out.print(datet);
						map.put("shopmallNum", map.get("shopmallNum")==null?orderNum:Integer.parseInt(map.get("shopmallNum").toString()) + orderNum);
					}

				}
			}

			//查询商城商品订单最近7天的数量
			List<OrderGoodItem> orderGoodItemList=indexService.queryWeekOrderGoodCount(groupIndexInDTO);
			for(OrderGoodItem orderGoodItem:orderGoodItemList){
				String datet =dateFormat.format(orderGoodItem.getGmtCreate());
				for(int k=0;k<resultlist.size();k++) {
					Map map = (HashMap) resultlist.get(k);
					int orderNum = orderGoodItem.getNumber();
					if(map.containsValue(datet)) {//已经存在该日期
						System.out.print(datet);
						map.put("shopmallNum", map.get("shopmallNum")==null?orderNum:Integer.parseInt(map.get("shopmallNum").toString()) + orderNum);
					}

				}
			}

			groupIndexListOutDTO.setOrderWeekList(resultlist);


		    groupIndexListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return groupIndexListOutDTO;
		}
		/*catch (ApplicationException ae) {
			groupIndexListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();

		}*/
		catch (Exception e) {
			groupIndexListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
		}
		return groupIndexListOutDTO;

	}



	@RequestMapping(value="/isGroupConfig")
	@ResponseBody
	public GroupConfigOutDTO isGroupConfig(@RequestBody GroupConfig groupConfig){

		GroupConfigOutDTO groupConfigOutDTO= new GroupConfigOutDTO();

		try {



			GroupConfig newObj=groupConfigService.getSystemParamsByGroupId(groupConfig.getGroupId());
			if (newObj!=null){
//				if(newObj.getFirstFeeRate()>0){
//					groupIndexListOutDTO.setIsInitial("1");
//				}else{
				groupConfigOutDTO.setIsInitial("1");
//				}

			}else{
				groupConfigOutDTO.setIsInitial("0");
			}

			groupConfigOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return groupConfigOutDTO;
		}
		/*catch (ApplicationException ae) {
			groupIndexListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();

		}*/
		catch (Exception e) {
			groupConfigOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return groupConfigOutDTO;

	}

}
